package com.lyf.core.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import androidx.annotation.NonNull;

import com.lxj.xpopup.XPopup;
import com.lyf.core.R;
import com.lyf.core.databinding.DialogAgilityBinding;

public class AgilityDialog extends BaseCenterPopup<DialogAgilityBinding> {


    private Builder builder;

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_agility;
    }

    private AgilityDialog(@NonNull Context context, Builder builder) {
        super(context);
        this.builder = builder;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        if (!TextUtils.isEmpty(builder.title)) {
            mViewBinding.tvTitle.setText(builder.title);
        }
        if (builder.titleTextColor > 0) {
            mViewBinding.tvTitle.setTextColor(builder.titleTextColor);
        }
        if (builder.titleTextSize > 0) {
            mViewBinding.tvTitle.setTextSize(builder.titleTextSize);
        }

        if (builder.showTitle) {
            mViewBinding.tvTitle.setVisibility(VISIBLE);
        } else {
            mViewBinding.tvTitle.setVisibility(GONE);
        }

        if (!builder.showSpanned) {
            if (!TextUtils.isEmpty(builder.content)) {
                mViewBinding.tvContent.setText(builder.content);
            }
            if (builder.contentTextColot > 0) {
                mViewBinding.tvContent.setTextColor(builder.contentTextColot);
            }
        } else {
            String content = builder.startContent + builder.content + builder.endContent;
            SpannableString spannableString = new SpannableString(content);
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3364")), content.indexOf(builder.content), content.indexOf(builder.endContent), 0);
            mViewBinding.tvContent.setText(spannableString);
        }
        if (builder.confirmTextSize > 0) {
            mViewBinding.tvContent.setTextSize(builder.confirmTextSize);
        }


        if (!TextUtils.isEmpty(builder.cancel)) {
            mViewBinding.btnCancel.setText(builder.cancel);
        }
        if (builder.cancelTextColor > 0) {
            mViewBinding.btnCancel.setTextColor(builder.cancelTextColor);
        }
        if (builder.cancelTextSize > 0) {
            mViewBinding.btnCancel.setTextSize(builder.cancelTextSize);
        }
        mViewBinding.btnCancel.setOnClickListener(v -> {
            dismiss();
            if (builder.onCancelListener != null) {
                builder.onCancelListener.onClick(v);
            }
        });
        if (builder.showCancelBtn) {
            mViewBinding.btnCancel.setVisibility(VISIBLE);
            mViewBinding.viewLine.setVisibility(VISIBLE);
        } else {
            mViewBinding.btnCancel.setVisibility(GONE);
            mViewBinding.viewLine.setVisibility(GONE);
        }


        if (!TextUtils.isEmpty(builder.confirm)) {
            mViewBinding.btnConfirm.setText(builder.confirm);
        }
        if (builder.confirmTxetColor != 0) {
            mViewBinding.btnConfirm.setTextColor(builder.confirmTxetColor);
        }
        if (builder.confirmTextSize > 0) {
            mViewBinding.btnConfirm.setTextSize(builder.confirmTextSize);
        }
        mViewBinding.btnConfirm.setOnClickListener(v -> {
            dismiss();
            if (builder.onConfirmListener != null) {
                builder.onConfirmListener.onClick(v);
            }
        });

    }

    @Override
    public DialogAgilityBinding getViewBinding() {
        return DialogAgilityBinding.bind(getContentView());
    }

    public void showDialog() {
        new XPopup.Builder(getContext())
                .autoOpenSoftInput(false)
                .isDestroyOnDismiss(true)
                .asCustom(this)
                .show();
    }

    public void showOnTouchOutsideDialog() {
        new XPopup.Builder(getContext())
                .autoOpenSoftInput(false)
                .dismissOnTouchOutside(false)
                .isDestroyOnDismiss(true)
                .asCustom(this)
                .show();
    }

    public static class Builder {
        String title;
        int titleTextColor;
        int titleTextSize;
        boolean showTitle = true;

        String content;
        int contentTextColot;
        int contentTextSize;
        String startContent;
        String endContent;
        boolean showSpanned = false;

        String cancel;
        int cancelTextColor;
        int cancelTextSize;
        boolean showCancelBtn = true;

        String confirm;
        int confirmTxetColor;
        int confirmTextSize;


        private OnClickListener onCancelListener;
        private OnClickListener onConfirmListener;


        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setTitleTextColor(int titleTextColor) {
            this.titleTextColor = titleTextColor;
            return this;
        }

        public Builder setTitleTextSize(int titleTextSize) {
            this.titleTextSize = titleTextSize;
            return this;
        }


        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setContentTextColot(int contentTextColot) {
            this.contentTextColot = contentTextColot;
            return this;
        }

        public Builder setContentTextSize(int contentTextSize) {
            this.contentTextSize = contentTextSize;
            return this;
        }

        public Builder setStartContent(String startContent) {
            this.startContent = startContent;
            return this;
        }

        public Builder setEndContent(String endContent) {
            this.endContent = endContent;
            return this;
        }

        public Builder isShowSpanned(boolean showSpanned) {
            this.showSpanned = showSpanned;
            return this;
        }

        public Builder isShowTitle(boolean showTitle) {
            this.showTitle = showTitle;
            return this;
        }

        public Builder setCancel(String cancel) {
            this.cancel = cancel;
            return this;
        }

        public Builder setCancelTextColor(int cancelTextColor) {
            this.cancelTextColor = cancelTextColor;
            return this;
        }

        public Builder setCancelTextSize(int cancelTextSize) {
            this.cancelTextSize = cancelTextSize;
            return this;
        }

        public Builder isShowCancelBtn(boolean showCancelBtn) {
            this.showCancelBtn = showCancelBtn;
            return this;
        }

        public Builder setConfirm(String confirm) {
            this.confirm = confirm;
            return this;
        }

        public Builder setConfirmTxetColor(int confirmTxetColor) {
            this.confirmTxetColor = confirmTxetColor;
            return this;
        }

        public Builder setConfirmTextSize(int confirmTextSize) {
            this.confirmTextSize = confirmTextSize;
            return this;
        }

        public Builder setOnCancelListener(OnClickListener onCancelListener) {
            this.onCancelListener = onCancelListener;
            return this;
        }

        public Builder setOnConfirmListener(OnClickListener onConfirmListener) {
            this.onConfirmListener = onConfirmListener;
            return this;
        }


        public AgilityDialog build(Context context) {
            AgilityDialog agilityDialog = new AgilityDialog(context, this);
            return agilityDialog;
        }
    }
}
